﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Net;
using System.IO;

namespace A5Site
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (Session["myuser"] == null)
                Label1.Text = " Welcome Unknown user";
            else
            {
                A5Lib.User u;
                u = (A5Lib.User)Session["myuser"];
                Label1.Text = "Welcome " + u.Username;

            }
            
            
        }

        protected override void OnLoadComplete(EventArgs e)
        {
            base.OnLoadComplete(e);
            Image1.ImageUrl = "http://www.opencaptcha.com/img/scom445" + DateTime.Now.Millisecond + ".jpgx";
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string username = txtUsername.Text;
            string password = txtPassword.Text;
            string captchatext = captchaResponse.Text;
           
            //check captcha
            char[] seps = {'/', '.'};
            string[] toks = Image1.ImageUrl.Split(seps);
            string imgName = toks[6];
            WebRequest request = WebRequest.Create("http://www.opencaptcha.com/validate.php?img=" + imgName + "&ans=" + captchatext);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream dataStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(dataStream);
            string responseFromServer = reader.ReadToEnd();
            reader.Close();
            dataStream.Close();
            response.Close();
            if (responseFromServer != "pass")
            {
                Label1.Text = "Failed Captcha";
                return;
            }
            else
            {
                Label1.Text = "";
            }


            UserServices.UserServicesClient myClient = new UserServices.UserServicesClient();
            if (myClient.ValidateUser(username, password))
            {
                A5Lib.User u = new A5Lib.User();
                //u.Username = username;
                //u.Password = password;
                UserServices.UserServicesClient myProxy = new UserServices.UserServicesClient();
                u = myProxy.GetUser(username);
                Session.Add("myuser", u); 
                Response.Redirect("User.aspx");
            }
            else if (FormsAuthentication.Authenticate(username, password)) 
             {
                 A5Lib.User u = new A5Lib.User();
                 u.Username = username;
                 u.Password = password;
                 Session.Add("myuser", u); 
                 Response.Redirect("Admin.aspx");
             }
             
            else
            {
                Label1.Text = "Login Failed";
      
            }
        }

        protected void btnHome_Click(object sender, EventArgs e)
        {
            Response.Redirect("Home.aspx");
        }

        protected void btnLogout_Click(object sender, EventArgs e)
        {
            Session.Abandon();
            Response.Redirect("Home.aspx");
        }



       
    }
}